package org.jeecg.modules.hxrencai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.hxrencai.bo.CollegeBo;
import org.jeecg.modules.hxrencai.bo.ProfessionListBo;
import org.jeecg.modules.hxrencai.bo.UniversityBo;
import org.jeecg.modules.hxrencai.entity.School;
import org.jeecg.modules.hxrencai.entity.StudyPark;
import org.jeecg.modules.hxrencai.entity.University;
import org.jeecg.modules.hxrencai.service.ISchoolService;
import org.jeecg.modules.hxrencai.service.IStudyParkService;
import org.jeecg.modules.hxrencai.service.IUniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 学校信息表
 * @Author: jeecg-boot
 * @Date: 2019-11-26
 * @Version: V1.0
 */
@Slf4j
//@Api(tags = "学校信息接口")
//@RestController
//@RequestMapping("/hxrencai/school")
public class SchoolController extends JeecgController<School, ISchoolService> {
    @Autowired
    private ISchoolService schoolService;
    @Autowired
    private IUniversityService universityService;
    @Autowired
    private IStudyParkService studyParkService;

    /**
     * 分页列表查询
     *
     * @param school
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
//    @AutoLog(value = "学校信息表-分页列表查询")
//    @ApiOperation(value = "学校信息表/园区/产业-分页列表查询", notes = "学校信息表/园区/产业-分页列表查询")
//    @GetMapping(value = "/list")
    public Result<?> queryPageList(School school,
                                   @ApiParam(value = "1 研究院 2 产业园区 3 本科院校 4 高职 5 中职 ")
                                   @RequestParam(value = "index", defaultValue = "1") Integer index,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        //QueryWrapper<School> queryWrapper = QueryGenerator.initQueryWrapper(school, req.getParameterMap());
        if (index == 3 || index == 4 || index == 5) {
            Page<School> page = new Page<School>(pageNo, pageSize);
            LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper();
            if (StringUtils.isNotBlank(school.getSchoolName())) {
                queryWrapper.like(School::getSchoolName, school.getSchoolName());
            }
            if (StringUtils.isNotBlank(school.getProvince())) {
                queryWrapper.like(School::getProvince, school.getProvince());
            }
            if (StringUtils.isNotBlank(school.getCity())) {
                queryWrapper.like(School::getCity, school.getCity());
            }
//            if (StringUtils.isNotBlank(school.getType())) {
//                queryWrapper.eq(School::getType, school.getType());
//            }
            if (index == 3) {
                queryWrapper.eq(School::getType, "1");
            } else if (index == 4) {
                queryWrapper.eq(School::getType, "2");
            } else if (index == 5) {
                queryWrapper.eq(School::getType, "3");
            }

            if (StringUtils.isNotBlank(school.getId())) {
                queryWrapper.eq(School::getId, school.getId());
            }
            IPage<School> pageList = schoolService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            LambdaQueryWrapper<StudyPark> queryWrapper = new LambdaQueryWrapper();
            if (index == 1) {
                queryWrapper.eq(StudyPark::getImportType, "3");
            } else {
                queryWrapper.eq(StudyPark::getImportType, "4");
            }
            if (StringUtils.isNotBlank(school.getProvince())) {
                queryWrapper.like(StudyPark::getArea, school.getProvince());
            }
            Page<StudyPark> page = new Page<StudyPark>(pageNo, pageSize);
            IPage<StudyPark> pageList = studyParkService.page(page, queryWrapper);
            return Result.ok(pageList);
        }
    }


    //    @ApiOperation(value = "学校专业列表-通过id查询", notes = "学校专业列表-通过id查询")
//    @GetMapping(value = "/queryProfessionById")
    public Result<?> queryProfessionById(@RequestParam(name = "id", required = true) String id) {
        School school = schoolService.getById(id);
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        List<University> universities = null;
        if ("福建省".equals(school.getProvince())) {
            //福建
            if ("1".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "2");
            } else if ("2".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "1");
            } else {
                queryWrapper.eq(University::getImportType, "6");
            }
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);
        } else {
            // 全国
            queryWrapper.eq(University::getImportType, "5");
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);
        }
        if (universities != null) {
            List<String> professions = universities.stream().map(University::getProfession).collect(Collectors.toList());
            return Result.ok(professions);
        } else {
            return Result.ok();
        }

    }

    //    @ApiOperation(value = "学校专业列表详情-通过id查询", notes = "学校专业列表详情-通过id查询")
//    @GetMapping(value = "/queryProfessionInfoById")
    public Result<?> queryProfessionInfoById(@RequestParam(name = "id", required = true) String id) {
        School school = schoolService.getById(id);
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        List<University> universities = null;
        if ("福建省".equals(school.getProvince())) {
            //福建
            if ("1".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "2");
            } else if ("2".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "1");
            } else {
                queryWrapper.eq(University::getImportType, "6");
            }
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);
        } else {
            // 全国
            queryWrapper.eq(University::getImportType, "5");
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);
        }
        return Result.ok(universities);
    }

    //    @ApiOperation(value = "学校专业列表详情分页-通过id查询", notes = "学校专业列表详情分页-通过id查询")
//    @GetMapping(value = "/queryProfessionInfoPageById")
    public Result<?> queryProfessionInfoPageById(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req,
                                                 @RequestParam(name = "id", required = true) String id) {
        School school = schoolService.getById(id);
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        List<University> universities = null;
        if ("福建省".equals(school.getProvince())) {
            //福建
            if ("1".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "2");
            } else if ("2".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "1");
            } else {
                queryWrapper.eq(University::getImportType, "6");
            }
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);
        } else if (school != null && StringUtils.isNotBlank(school.getProvince())) {
            // 全国
            queryWrapper.eq(University::getImportType, "5");
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);
        }
        IPage<University> pageList = new Page<University>();
        pageList.setTotal(universities.size());
        pageList.setCurrent(pageNo);
        pageList.setSize(pageSize);
        if (universities != null) {
            List<University> list = universities.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
            pageList.setRecords(list);
        } else {
            pageList.setRecords(null);
        }
        return Result.ok(pageList);
    }


    //    @ApiOperation(value = "学校学院专业分页-通过id查询 默认显示三个学院", notes = "学校学院专业分页-通过id查询")
//    @GetMapping(value = "/querySchoolPageById")
    public Result<UniversityBo> querySchoolPageById(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req,
                                                    @RequestParam(name = "id", required = true) String id) {
        Result<UniversityBo> result = new Result<>();
        School school = schoolService.getById(id);
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        List<University> universities = null;
        UniversityBo universityBo = new UniversityBo();
        if (school == null) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("学校不存在");
            return result;
        }
        universityBo.setSchoolName(school.getSchoolName());
        universityBo.setId(school.getId());
        List<String> colleges = new ArrayList<>();
        List<CollegeBo> collegeList = new ArrayList<>();
        if ("福建省".equals(school.getProvince())) {
            //福建
            if ("1".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "2");
            } else if ("2".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "1");
            } else if ("3".equals(school.getType())) {
                queryWrapper.eq(University::getImportType, "6");
            }
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);

        } else if (school != null && StringUtils.isNotBlank(school.getProvince())) {
            // 全国
            queryWrapper.eq(University::getImportType, "5");
            queryWrapper.eq(University::getSchoolName, school.getSchoolName());
            universities = universityService.list(queryWrapper);
        }
        Set<String> strings = new HashSet<>();
        for (University university : universities) {
            if (StringUtils.isBlank(university.getCollege())) {
                university.setCollege("专业");
            }
            if (strings.contains(university.getCollege())) {
                for (CollegeBo collegeBo : collegeList) {
                    if (collegeBo.getCollege().equals(university.getCollege())) {
                        List<ProfessionListBo> hasBuild = collegeBo.getProfessions();
                        ProfessionListBo professionListBo = new ProfessionListBo();
                        professionListBo.setProfession(university.getProfession());
                        if ("1".equals(university.getIsMerge())) {
                            professionListBo.setProfessionNum("并记" + university.getProfessionNum());
                        } else {
                            professionListBo.setProfessionNum(university.getProfessionNum());
                        }
                        hasBuild.add(professionListBo);
                    }
                }

            } else {
                strings.add(university.getCollege());
                colleges.add(university.getCollege());
                CollegeBo collegeBo = new CollegeBo();
                collegeBo.setCollege(university.getCollege());
                List<ProfessionListBo> professions = new ArrayList<>();
                ProfessionListBo professionListBo = new ProfessionListBo();
                professionListBo.setProfession(university.getProfession());
                if ("1".equals(university.getIsMerge())) {
                    professionListBo.setProfessionNum("并记" + university.getProfessionNum());
                } else {
                    professionListBo.setProfessionNum(university.getProfessionNum());
                }
                professions.add(professionListBo);
                collegeBo.setProfessions(professions);
                collegeList.add(collegeBo);
            }
        }

        colleges = colleges.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        collegeList = collegeList.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());

        universityBo.setColleges(colleges);
        universityBo.setCollegeList(collegeList);
        universityBo.setTotal(collegeList.size());
        universityBo.setCurrent(pageNo);
        universityBo.setTotal(pageSize);
//        IPage<University> pageList = new Page<University>();
//        pageList.setTotal(universities.size());
//        pageList.setCurrent(pageNo);
//        pageList.setSize(pageSize);
//        if (universities != null) {
//            List<University> list = universities.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
//            pageList.setRecords(list);
//        } else {
//            pageList.setRecords(null);
//        }
        result.setSuccess(true);
        result.setResult(universityBo);
        result.setCode(200);
        result.setMessage("查询成功");
        return result;
    }


    /**
     * 添加
     *
     * @param school
     * @return
     */
//    @AutoLog(value = "学校信息表-添加")
    //@ApiOperation(value="学校信息表-添加", notes="学校信息表-添加")
//    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody School school) {
        schoolService.save(school);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param school
     * @return
     */
//    @AutoLog(value = "学校信息表-编辑")
    //@ApiOperation(value="学校信息表-编辑", notes="学校信息表-编辑")
//    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody School school) {
        schoolService.updateById(school);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "学校信息表-通过id删除")
    //@ApiOperation(value="学校信息表-通过id删除", notes="学校信息表-通过id删除")
//    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        schoolService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "学校信息表-批量删除")
    //@ApiOperation(value="学校信息表-批量删除", notes="学校信息表-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.schoolService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "学校信息表-通过id查询")
    //@ApiOperation(value="学校信息表-通过id查询", notes="学校信息表-通过id查询")
//    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        School school = schoolService.getById(id);
        return Result.ok(school);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param school
     */
//    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, School school) {
        return super.exportXls(request, school, School.class, "学校信息表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, School.class);
    }

}
